﻿using BaseUi.Views.MainWindows;
using HandyControl.Controls;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi.Views.PlcView
{
    /// <summary>
    /// BujinView.xaml 的交互逻辑步进页面
    /// </summary>
    public partial class BujinView : UserControlBase
    {
        private StudentData viewModel = new StudentData();
        private MachbujinData bujinModel = new MachbujinData();
        public SolidColorBrush BoroColor = new SolidColorBrush(Color.FromRgb(220, 220, 220));
        public SolidColorBrush GreenColor = new SolidColorBrush(Color.FromRgb(173, 255, 47));
        private bool bujin = false;
        public BujinView()
        {
            InitializeComponent();
            DataContext = bujinModel;
            bujinModel.CommonViewModel = MainWindow4.viewModel.CommonViewModel;
            viewModel.CommonViewModel = MainWindow4.viewModel.CommonViewModel;
        }
        private void loadFirst(object sender, EventArgs e)
        {
            changclor(bujin);
            getbujinv(); 
            getyundong();
            getdinwei();
            Task Sbujin = new Task(() =>
            {
                while (true)
                {
                    getnewshud();
                    Thread.Sleep(200);
                }
            });
            Sbujin.Start();
        }
        /// <summary>
        /// 定位位置
        /// </summary>
        private void getdinwei()
        {
            var ioi = LocalStatic.Plc.Read<float>("1472");
            if (ioi.IsSucceed == false)
            {
                BujinBox2g1z1.Text = "未接收";
            }
            else
            {
                BujinBox2g1z1.Text = ioi.Value.ToString();
            }

        }
        /// <summary>
        /// 步进距离
        /// </summary>
        private void getbujinv()
        {
            var io2 = LocalStatic.Plc.Read<float>("1466");
            if (io2.IsSucceed == false)
            {
                BuBox1g1z1.Text = "未接收";
            }
            else { BuBox1g1z1.Text = io2.Value.ToString(); }
        }
        private void changclor(bool bucolor)
        {
            if (bujin)
            {
                butjog.Background = BoroColor;
                butbujin.Background = GreenColor;
            }
            else
            {
                butjog.Background = GreenColor;
                butbujin.Background = BoroColor;
            }
        }
        private void getveloc(int inder)
        {
            veloc1.Background = BoroColor;
            veloc2.Background = BoroColor;
            veloc3.Background = BoroColor;
            if (inder == 0)
            {

                veloc1.Background = GreenColor;
            }
            else if (inder == 1)
            {

                veloc2.Background = GreenColor;
            }
            else if (inder == 2)
            {

                veloc3.Background = GreenColor;
            }
        }
        /// <summary>
        /// 运动允许误差
        /// </summary>
        private void getyundong()
        {
            var io2 = LocalStatic.Plc.Read<float>("1806");
            if (io2.IsSucceed == false)
            {
                BujinBox2g2z1.Text = "未接收";
            }
            else { BujinBox2g2z1.Text = io2.Value.ToString(); }
        }
        /// <summary>
        /// 当前速度,位置
        /// </summary>
        private void getnewshud()
        {
            var ioi = LocalStatic.Plc.Read<float>("40");
            if (ioi.IsSucceed == false)
            {
                bujinModel.sudru = "未接收";
            }
            else
            {
                bujinModel.sudru = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("42"); 
            if (ioi.IsSucceed == false)
            {
                bujinModel.sudcu = "未接收";
            }
            else
            {
                bujinModel.sudcu = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("44");
            if (ioi.IsSucceed == false)
            {
                bujinModel.sudtu = "未接收";
            }
            else
            {
                bujinModel.sudtu = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("46");
            if (ioi.IsSucceed == false)
            {
                bujinModel.sudsu = "未接收";
            }
            else
            {
                bujinModel.sudsu = ioi.Value.ToString();
            }
            ioi = LocalStatic.Plc.Read<float>("10");
            if (ioi.IsSucceed == false)
            {
                bujinModel.weizhitu = "未接收";
            }
            else
            {
                bujinModel.weizhitu = ioi.Value.ToString();
            }
        }
        

        private void BujinButton_Click1g1z1(object sender, RoutedEventArgs e)
        {
            bujin = false;
            changclor(bujin);
        }

        private void BujinButton_Click1g1z2(object sender, RoutedEventArgs e)
        {
            bujin = true;
            changclor(bujin);
            getbujinv();
        }
        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BujinButton_Click2g1z1(object sender, RoutedEventArgs e)
        {
            try
            {

                var ioi = LocalStatic.Plc.Write<bool>("24", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }
        /// <summary>
        /// 定位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void BujinButton_Click2g1z2(object sender, RoutedEventArgs e)
        {
            try
            {

                var ioi = LocalStatic.Plc.Write<bool>("25", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }

        }

        private void BujinButton_Click3g2z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<Int16>("5", 0);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void BujinButton_Click3g2z2(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<Int16>("5", 1);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void BujinButton_Click3g2z3(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<Int16>("5", 2);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void BujinButton_Click3g3z1(object sender, RoutedEventArgs e)
        {
            try
            {
                var ioi = LocalStatic.Plc.Write<bool>("111", true);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }
        
        private void Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                short ni = 1;
                if (bujintogg.IsChecked == true)
                {
                    ni = 0;
                }
                else
                {
                    ni = 1;
                }
                var ioi = LocalStatic.Plc.Write<Int16>("1000", ni);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach1g1d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("29", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    
                        Growl.Error("无扫码步进");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach1g1p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("29", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach1g2d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("30", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach1g2p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("30", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach2g1d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("22", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    float fait = float.Parse(BuBox1g1z1.Text);
                    if (fait<0)
                    {
                        fait = fait * (-1);
                    }
                    var ioi1 = LocalStatic.Plc.Write<float>("1474", fait);
                    if (ioi1.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                    var ioi = LocalStatic.Plc.Write<bool>("26", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach2g1p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("22", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach3g1d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("19", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    Growl.Error("入料门开无步进");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach3g1p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("19", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach4g1d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("141", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    Growl.Error("出料门开无步进");
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach4g1p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("141", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach5g1d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("23", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {
                    float fait = float.Parse(BuBox1g1z1.Text);
                    if (fait > 0)
                    {
                        fait = fait * (-1);
                    }
                    var ioi1 = LocalStatic.Plc.Write<float>("1474", fait);
                    if (ioi1.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                    var ioi = LocalStatic.Plc.Write<bool>("26", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach5g1p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("23", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach4g2d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("140", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach4g2p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("140", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach3g2d(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("18", true);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void bujinmach3g2p(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (!bujin)
                {

                    var ioi = LocalStatic.Plc.Write<bool>("18", false);
                    if (ioi.IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }

                }
                else
                {

                }
            }
            catch (Exception ee)
            {
                Growl.Error(ee.ToString());
            }
        }

        private void yundongwuctu(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(BujinBox2g2z1.Text);
                var ioi = LocalStatic.Plc.Write<float>("1810", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }

        private void TextBox_KeyDowntu(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    float fait = float.Parse(BujinBox2g1z1.Text);
                    if (LocalStatic.Plc.Write<float>("1472", fait).IsSucceed == false)
                    {
                        Growl.Error("上传失败");
                    }
                }
                catch (Exception ee)
                {
                    Growl.Error(ee.ToString());
                }
            }
        }

        private void bujintu(object sender, MouseButtonEventArgs e)
        {
            try
            {
                float fait = float.Parse(BuBox1g1z1.Text);
                var ioi = LocalStatic.Plc.Write<float>("1474", fait);
                if (ioi.IsSucceed == false)
                {
                    Growl.Error("上传失败");
                }
            }
            catch (Exception ee)
            {

                Growl.Error(ee.ToString());
            }
        }
    }
}
